//
//  SRHistoryDataSR03+OtherCalc.m
//  CareRingApp
//
//  Created by Linktop on 2022/8/19.
//

#import "SRHistoryDataSR03+OtherCalc.h"

@implementation SRHistoryDataSR03 (OtherCalc)

-(NSNumber *)calcHRv
{
    NSNumber *hrvResult = nil;
    NSMutableArray<NSNumber *> *rawHr = [NSMutableArray new];
    if (self.heart_ori_1 ) {
        [rawHr addObject:self.heart_ori_1];
    }
    if (self.heart_ori_2 ) {
        [rawHr addObject:self.heart_ori_2];
    }
    if (self.heart_ori_3 ) {
        [rawHr addObject:self.heart_ori_3];
    }
    
    if (rawHr.count < 2)
    {
        return hrvResult;
    }
    
    NSMutableArray<NSNumber *> * list = rawHr;
    
    NSUInteger size = list.count;
    double hr = 0.0;
    double hrv = 0.0;
    int rrLst = 0;
    
    for (int i = 0; i < list.count; i++) {
        double hrCur = list[i].doubleValue;
        double rrCur = 60000.0 / hrCur; // 一分钟6万毫秒
        hr += hrCur;
        if (rrLst > 0) {
            hrv +=pow((rrCur - rrLst), 2.0);
//            hrv += .pow(2.0)
        }
        rrLst = rrCur;
    }
    hr /= size;
    hrv /= (size - 1);
    if (hr >= (self.deviceModifyHeartRate.intValue - 1)
         && hr <= (self.deviceModifyHeartRate.intValue + 1)) {
        
        hrvResult = @(  (int)(round( sqrt(hrv))) );
    }
    
    return hrvResult;
    
}

+(NSData *)historyDataToJson:(NSArray<SRHistoryDataSR03 *> *)dataArray
{
    NSData *json = nil;
    if (!dataArray.count) {
        return nil;
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssXXX";
    NSMutableArray *jsonArray = [NSMutableArray new];
    for (int i = 0; i < dataArray.count; i++) {
        SRHistoryDataSR03 *srdata = dataArray[i];
        
        NSMutableDictionary *mutDict = [NSMutableDictionary new];
        if (srdata.timeStamp) {
            mutDict[@"ts"] = srdata.timeStamp; // 时间戳
            mutDict[@"date"] = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:srdata.timeStamp.doubleValue]]; // 日期
        }
        
      
        if ( srdata.soprtTimes) {
            mutDict[@"motion"] = srdata.soprtTimes; //运动计数

        }
        if (srdata.deviceModifyHeartRate) {
            mutDict[@"hr"] = srdata.deviceModifyHeartRate; // 处理后的心率

        }
        if (srdata.heart_ori_1 && srdata.heart_ori_2) {
            mutDict[@"rawHr"] = [NSString stringWithFormat:@"%@,%@,%@", srdata.heart_ori_1, srdata.heart_ori_2, srdata.heart_ori_3 != nil ? srdata.heart_ori_3 : @""]; //原始心率

        }
        
        mutDict[@"spo2"] = srdata.bloodOxygen ? srdata.bloodOxygen : @(0); //血氧
        if (srdata.steps) {
            mutDict[@"steps"] = srdata.steps; //计步

        }
        if (srdata.skinTemp) {
            mutDict[@"skinTc"] = srdata.skinTemp; // 体温

        }
        [jsonArray addObject:mutDict];
    }
    
    NSError *error;
    json = [NSJSONSerialization dataWithJSONObject:jsonArray options:NSJSONWritingPrettyPrinted error:&error];
    
    
    return json;
    
}


@end
